package com.leps.nq.bu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leps.nq.bu.entity.BuStation;
import com.leps.nq.bu.mapper.*;
import com.leps.nq.bu.service.IBuStationService;
import com.leps.nq.bu.vo.StationQueryModel;
import com.leps.nq.bu.vo.StationVo;
import com.leps.nq.common.util.SecurityUtil;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.op.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 区块表 服务实现类
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@Service
public class BuStationServiceImpl extends ServiceImpl<BuStationMapper, BuStation> implements IBuStationService {
    @Autowired
    private BuStationMapper buStationMapper;
    @Autowired
    private BuPlcMapper buPlcMapper;
    @Autowired
    private BuPumpMapper buPumpMapper;
    @Autowired
    private BuWellMapper buWellMapper;
    @Autowired
    private OpReplenishMapper replenishMapper;
    @Autowired
    private OpAlarmMapper alarmMapper;
    @Autowired
    private OpInspectionMapper inspectionMapper;
    @Autowired
    private OpRetireMapper retireMapper;
    @Autowired
    private OpWaterTestMapper waterTestMapper;
    @Autowired
    private OpBubbleReplenishMapper bubbleReplenishMapper;
    @Autowired
    private OpDeviceMaintenanceMapper deviceMaintenanceMapper;
    @Autowired
    private BuUserStationMapper userStationMapper;

    @Override
    public PageVo<StationVo> pageByQueryModel(StationQueryModel queryModel) {
        Page<StationVo> page = Page.of(queryModel.getPageNum(), queryModel.getPageSize());
        page = this.buStationMapper.selectByQueryModel(page, queryModel);
        return PageVo.of(page);
    }

    @Override
    @Transactional
    public boolean updateCascade(BuStation station) {
        final int stationId = station.getStationId();
        final String stationName = station.getStationName();
        final String origin = this.buStationMapper.selectById(stationId).getStationName();
        station.setUpdateBy(SecurityUtil.getUserName());
        station.setUpdateTime(new Date());
        this.buStationMapper.updateById(station);
        this.buStationMapper.updateRedundantField(station);
        // cascade update if neccissary
        if(Objects.equals(origin, stationName)) return true;
        this.buPlcMapper.updateStationName(stationName, stationId);
        this.buPumpMapper.updateStationName(stationName, stationId);
        this.buWellMapper.updateStationName(stationName, stationId);
        this.alarmMapper.updateProperty("station_name", stationName, "station_id", stationId);
        this.bubbleReplenishMapper.updateProperty("station_name", stationName, "station_id", stationId);
        this.inspectionMapper.updateProperty("station_name", stationName, "station_id", stationId);
        this.replenishMapper.updateProperty("station_name", stationName, "station_id", stationId);
        this.retireMapper.updateProperty("station_name", stationName, "station_id", stationId);
        this.waterTestMapper.updateProperty("station_name", stationName, "station_id", stationId);
        return true;
    }

    @Override
    public boolean save(BuStation entity) {
        super.save(entity);
        // 自动分配上级客户管理员和区块管理员对该平台的数据权限
        this.userStationMapper.insertBatchFromUserCustRelationAndUserAreaRelation(entity);
        return true;
    }

    @Override
    @Transactional
    public boolean saveAndFillRedundantField(BuStation station) {
        station.setCreateBy(SecurityUtil.getUserName());
        station.setCreateTime(new Date());
        this.buStationMapper.insert(station);
        this.buStationMapper.updateRedundantField(station);
        // 自动分配上级客户管理员和区块管理员对该平台的数据权限
        this.userStationMapper.insertBatchFromUserCustRelationAndUserAreaRelation(station);
        return true;
    }

    @Override
    public List<BuStation> listForSelect(Integer areaId) {
        if(SecurityUtil.isAdmin()) {
            return this.lambdaQuery().eq(BuStation::getAreaId, areaId).list();
        }
        long userId = SecurityUtil.getUserId();
        return this.buStationMapper.selectListByAreaIdAndUserId(areaId, userId);
    }
}
